package com.example.yangguoying;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.yangguoying.SqLiteDatabase.GetDatabase;
import com.example.yangguoying.entity.StudentInfo;
import com.example.yangguoying.util.SolveEditTextScrollClash;

public class ModifyInfoActivity extends AppCompatActivity {

    EditText modify_student_name;
    EditText modify_student_num;
    EditText modify_student_phone;
    EditText modify_student_faculty;
    EditText modify_student_grade;
    EditText modify_student_class;
    EditText modify_student_dormitory;
    EditText modify_student_address;
    EditText modify_student_remarks;
    Button modify_student_info;
    GetDatabase database;
    SQLiteDatabase sqLiteDatabase;
    Cursor cursor;
    ContentValues contentValues;
    Intent globalIntent;
    StudentInfo studentInfo;
    ImageButton bt_bak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_info_activity);
        
        initView();
        setView();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        modify_student_name = findViewById(R.id.modify_student_name);
        modify_student_num = findViewById(R.id.modify_student_num);
        modify_student_phone = findViewById(R.id.modify_student_phone);
        modify_student_faculty = findViewById(R.id.modify_student_faculty);
        modify_student_grade = findViewById(R.id.modify_student_grade);
        modify_student_class = findViewById(R.id.modify_student_class);
        modify_student_dormitory = findViewById(R.id.modify_student_dormitory);
        modify_student_address = findViewById(R.id.modify_student_address);
        modify_student_remarks = findViewById(R.id.modify_student_remarks);
        modify_student_info = findViewById(R.id.modify_student_info);
        contentValues = new ContentValues();
        bt_bak = findViewById(R.id.bt_back);
        modify_student_remarks.setOnTouchListener(new SolveEditTextScrollClash(modify_student_remarks));
        database = new GetDatabase();
        sqLiteDatabase = database.getDatabase(this).getWritableDatabase();
        globalIntent = getIntent();
        studentInfo = queryStudentInfo(globalIntent.getIntExtra("id",-1));
        setShowStudentInfo(studentInfo);
    }

    @Override
    public void onBackPressed() {
        if (judgmentModify()){
            showAlertDialog();
        }else{
            finish();
        }
    }

    private void setView() {
        modify_student_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 修改数据
                modifyInfo();
            }
        });

        bt_bak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (judgmentModify()){
                    showAlertDialog();
                }else{
                    finish();
                }
            }
        });
    }

    private String getEditTextString(EditText editText){
        return editText.getText().toString();
    }

    private StudentInfo queryStudentInfo(int id){
        StudentInfo queryInfo = null;
        cursor = sqLiteDatabase.query("student_info",null,"student_info_id = ?",new String[]{String.valueOf(id)},null,null,null,null);
        if (cursor.moveToFirst()) {
            do {
                int studentInfoId = cursor.getInt(cursor.getColumnIndex("student_info_id"));
                String studentName = cursor.getString(cursor.getColumnIndex("student_name"));
                String studentNum = cursor.getString(cursor.getColumnIndex("student_num"));
                String studentPhone = cursor.getString(cursor.getColumnIndex("student_phone"));
                String studentAddress = cursor.getString(cursor.getColumnIndex("student_address"));
                String studentFaculty = cursor.getString(cursor.getColumnIndex("student_faculty"));
                String studentGrade = cursor.getString(cursor.getColumnIndex("student_grade"));
                String studentClass = cursor.getString(cursor.getColumnIndex("student_class"));
                String studentDormitory = cursor.getString(cursor.getColumnIndex("student_dormitory"));
                String studentRemarks = cursor.getString(cursor.getColumnIndex("student_remarks"));
                int isUpload = cursor.getInt(cursor.getColumnIndex("is_upload"));
                queryInfo = new StudentInfo(studentInfoId,studentName,studentNum,studentPhone,studentAddress,studentFaculty,studentGrade,studentClass,studentDormitory,studentRemarks,isUpload);
            } while (cursor.moveToNext());

        }
        return  queryInfo;
    }

    private void setShowStudentInfo(StudentInfo studentInfo){
        modify_student_name.setText(studentInfo.getStudentName());
        modify_student_num.setText(studentInfo.getStudentNum());
        modify_student_phone.setText(studentInfo.getStudentPhone());
        modify_student_faculty.setText(studentInfo.getStudentFaculty());
        modify_student_grade.setText(studentInfo.getStudentGrade());
        modify_student_class.setText(studentInfo.getStudentClass());
        modify_student_dormitory.setText(studentInfo.getStudentDormitory());
        modify_student_address.setText(studentInfo.getStudentAddress());
        modify_student_remarks.setText(studentInfo.getStudentRemarks());
    }

    private void modifyInfo(){
        String studentName = getEditTextString(modify_student_name);
        String studentNum = getEditTextString(modify_student_num);
        String studentPhone = getEditTextString(modify_student_phone);
        String studentAddress = getEditTextString(modify_student_address);
        String studentFaculty = getEditTextString(modify_student_faculty);
        String studentGrade = getEditTextString(modify_student_grade);
        String studentClass = getEditTextString(modify_student_class);
        String studentDormitory = getEditTextString(modify_student_dormitory);
        String studentRemarks = getEditTextString(modify_student_remarks);

        if (!studentName.equals("") && !studentNum.equals("") && !studentPhone.equals("") && !studentAddress.equals("") && !studentFaculty.equals("") && !studentGrade.equals("") && !studentClass.equals("") && !studentDormitory.equals("") && !studentRemarks.equals("")) {
            if (judgmentModify()){
                //TODO 更新数据
                contentValues.put("student_name",studentName);
                contentValues.put("student_num",studentNum);
                contentValues.put("student_phone",studentPhone);
                contentValues.put("student_address",studentAddress);
                contentValues.put("student_faculty",studentFaculty);
                contentValues.put("student_grade",studentGrade);
                contentValues.put("student_class",studentClass);
                contentValues.put("student_dormitory",studentDormitory);
                contentValues.put("student_remarks",studentRemarks);
                contentValues.put("is_upload", 0);
                //TODO 设置id
                sqLiteDatabase.update("student_info",contentValues,"student_info_id = ?",new String[]{String.valueOf(globalIntent.getIntExtra("id",-1))});
                contentValues.clear();
                Toast.makeText(ModifyInfoActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finish();
            }
        }else{
            //TODO 删除数据
            AlertDialog.Builder builder = new AlertDialog.Builder(ModifyInfoActivity.this);
            builder.setTitle("提示");
            builder.setMessage("是否删除数据！");
            builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    sqLiteDatabase.delete("student_info","student_info_id = ?",new String[]{String.valueOf(globalIntent.getIntExtra("id",-1))});
                    Toast.makeText(ModifyInfoActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    finish();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            builder.show();


        }
    }

    private boolean judgmentModify(){
        String studentName = getEditTextString(modify_student_name);
        String studentNum = getEditTextString(modify_student_num);
        String studentPhone = getEditTextString(modify_student_phone);
        String studentAddress = getEditTextString(modify_student_address);
        String studentFaculty = getEditTextString(modify_student_faculty);
        String studentGrade = getEditTextString(modify_student_grade);
        String studentClass = getEditTextString(modify_student_class);
        String studentDormitory = getEditTextString(modify_student_dormitory);
        String studentRemarks = getEditTextString(modify_student_remarks);
        System.out.println(studentInfo.getStudentName());
        System.out.println(studentName);
        return !studentName.equals(studentInfo.getStudentName()) || !studentNum.equals(studentInfo.getStudentNum()) || !studentPhone.equals(studentInfo.getStudentPhone()) ||
                !studentAddress.equals(studentInfo.getStudentAddress()) || !studentFaculty.equals(studentInfo.getStudentFaculty()) || !studentGrade.equals(studentInfo.getStudentGrade()) ||
                !studentClass.equals(studentInfo.getStudentClass()) || !studentDormitory.equals(studentInfo.getStudentDormitory()) || !studentRemarks.equals(studentInfo.getStudentRemarks());
    }

    private void showAlertDialog(){
            AlertDialog.Builder builder = new AlertDialog.Builder(ModifyInfoActivity.this);
//           设置提示框标题
            builder.setTitle("提示");
//            设置提示框提示文字
            builder.setMessage("是否保存修改的数据！");
//            监听提示框点击保存按钮
            builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
//                    保存数据到数据库方法
                    modifyInfo();
                }
            });
//            监听用户点击提示框取消按钮
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
//            让提示框显示出来
            builder.show();

    }
}